package concurrent.threadPool;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class SchedulerTest {

	public static void main(String[] args) {
		
		ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
//		service.scheduleAtFixedRate(() -> {
//			System.out.println(new SimpleDateFormat("HH:mm:ss").format(new Date()));
//			if(new Random().nextBoolean()){
//				//报错之后，此任务将不再执行
//				throw new RuntimeException("ASDFASDFASD");
//			}
//		}, 0, 1, TimeUnit.SECONDS);
		service.scheduleAtFixedRate(() -> {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("1、" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
		}, 0, 1, TimeUnit.SECONDS);
		service.scheduleAtFixedRate(() -> {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("2、" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
		}, 0, 1, TimeUnit.SECONDS);
	}
}
